#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using TVC.Modules.CustomControl;

#endregion

namespace TVC.Modules.GoodsMatching
{
    public partial class Split : Form
    {
        public string type;
        private string TRANS_LINE;
        private string _GRN_REF;
        public Split()
        {
            InitializeComponent();
            initSplit();
        }
        public DataRow drItemOld { get; set; }
        public MainForm MainForm { get; set; }
        public DataRow drItemCode { get; set; }
        public DataTable dtReturn { get; set; }
        public DataTable dtPO { get; set; }
        public DataTable dtValidate { get; set; }
        int OtherValue = 0; 

        public Split(MainForm frm, DataRow dr, DataTable dtResoure, DataTable dt, string OValue)
        {
            InitializeComponent();
            MainForm = frm;
            dtPO = dtResoure;
            dtValidate = dt;
            drItemCode = dr;
            initSplit();
            //OtherValue = int.Parse(OValue);
        }

        private void AddItem(string TRANS_LINE, string Itemcode, string desc, decimal amount)
        {
            ListViewItem objItem = new ListViewItem();
            Label txTRANS_LINE = new Label();
            txTRANS_LINE.Text = TRANS_LINE;
            txTRANS_LINE.Name = "TRANS_LINE";
            txTRANS_LINE.Tag = objItem;
            objItem.SubItems.Add(txTRANS_LINE);

            Label txItemCode = new Label();
            txItemCode.Text = Itemcode;
            txItemCode.Name = "Description";
            txItemCode.Tag = objItem;
            objItem.SubItems.Add(txItemCode);

            Label txDescription = new Label();
            txDescription.Text = desc;
            txDescription.Name = "Description";
            txDescription.Tag = objItem;
            objItem.SubItems.Add(txDescription);

            Label amt = new Label();
            //amt.Minimum = txtAmount.Minimum;
            //amt.Maximum = txtAmount.Maximum;
            //amt.DecimalPlaces = txtAmount.DecimalPlaces;
            amt.Text = amount.ToString();
            //amt.ThousandsSeparator = txtAmount.ThousandsSeparator;
            amt.Name = "Amount";
            amt.Tag = objItem;
            objItem.SubItems.Add(amt);
            lvSplit.Items.Add(objItem);
        }

        private void initSplit()
        {
            if (dtReturn == null)
                dtReturn = dtPO.Clone();
            if (drItemCode != null)
            {
                txtAmount.Value = Convert.ToDecimal(drItemCode["PURCH_QTY"].ToString());
                AddItem(drItemCode["TRANS_LINE"].ToString(), drItemCode["ITEM_CODE"].ToString(), drItemCode["SUPP_DESCR"].ToString(), txtAmount.Value);
            }
        }

        private void btSplit_Click(object sender, EventArgs e)
        {
            
            if (lvSplit.CheckedItems.Count > 0)
            {
                //DataRow dr = dtPO.NewRow();

                if (nfPercent.Value >= 1)
                {
                    //for (int i = 0; i < lvSplit.CheckedItems.Count; i++)
                    //{
                    int i = 0;
                    ListViewItem objItem = lvSplit.CheckedItems[i] as ListViewItem;
                    TRANS_LINE = (((ListViewItem.ListViewSubControlItem)objItem.SubItems[0]).Control).Text.Trim();// drItemCode["TRANS_LINE"].ToString();
                    //if (int.Parse((TRANS_LINE + "000").Substring(3, 1)) == 9)
                    //{
                    //    //MessageBox.Show("Quantity number > 0 ");
                    //    return;
                    //}
                    string NumBegin = TRANS_LINE.Substring(0, 3);
                    string NumEnd = TRANS_LINE.Substring(TRANS_LINE.Length - 1, 1);
                    try
                    {
                        DataRow[] dr = dtValidate.Select("TRANS_LINE like '" + NumBegin + "%'  and TRANS_LINE <> '" + NumBegin + "'");
                        DataRow[] drdgv = dtPO.Select("TRANS_LINE like '" + NumBegin + "%'  and TRANS_LINE <> '" + NumBegin + "'");
                        if (drdgv.Length > 0)
                        {
                            int Max = int.Parse(NumBegin);
                            for (int j = 0; j < dr.Length; j++)
                            {
                                if (int.Parse(dr[j]["TRANS_LINE"].ToString()) >= Max)
                                    Max = int.Parse(dr[j]["TRANS_LINE"].ToString());
                            }
                            int Maxdgv = int.Parse(NumBegin);
                            for (int j = 0; j < drdgv.Length; j++)
                            {
                                if (int.Parse(drdgv[j]["TRANS_LINE"].ToString()) >= Max)
                                    Maxdgv = int.Parse(drdgv[j]["TRANS_LINE"].ToString());
                            }
                            int lvIMax = int.Parse(NumBegin);
                            foreach (ListViewItem item in lvSplit.Items)
                            {
                                string LineNume = (((ListViewItem.ListViewSubControlItem)item.SubItems[0]).Control).Text;
                                if (int.Parse(LineNume) >= lvIMax)
                                    lvIMax = int.Parse(LineNume);
                            }
                            
                            if (lvIMax >= Maxdgv && lvIMax > Max)
                            {
                                //if (lvIMax.ToString)
                                
                                if (lvIMax.ToString().Length == 2)
                                {
                                    if (lvIMax.ToString().Substring(1, 1) == "9")
                                    {
                                        MessageBox.Show(" Maximum split / over delivery lines already processed for this item line ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, false);
                                        return;
                                    }
                                }
                                if (int.Parse(TRANS_LINE) <= lvIMax && TRANS_LINE.Length >= 3)
                                    TRANS_LINE = "00" + (lvIMax + 1).ToString();
                                if (TRANS_LINE.Length == 3)
                                    TRANS_LINE = NumBegin + "1";
                            }
                            else if (Maxdgv >= lvIMax && Maxdgv > Max)
                            {
                                if (Maxdgv.ToString().Length == 2)
                                {
                                    if (Maxdgv.ToString().Substring(1, 1) == "9")
                                    {
                                        MessageBox.Show(" Maximum split / over delivery lines already processed for this item line ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, false);
                                        return;
                                    }
                                }
                                if (int.Parse(TRANS_LINE) <= Maxdgv && TRANS_LINE.Length >= 3)
                                    TRANS_LINE = "00" + (Maxdgv + 1).ToString();
                                if (TRANS_LINE.Length == 3)
                                    TRANS_LINE = NumBegin + "1";
                            }
                            else
                            {
                                if (Max.ToString().Length == 2)
                                {
                                    if (Max.ToString().Substring(1, 1) == "9")
                                    {
                                        MessageBox.Show(" Maximum split / over delivery lines already processed for this item line ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, false);
                                        return;
                                    }
                                }
                                TRANS_LINE = "00" + (Max + 1).ToString();
                            }
                        }
                        else
                        {

                            int Max = int.Parse(NumBegin);
                            foreach (ListViewItem item in lvSplit.Items)
                            {
                                string LineNume = (((ListViewItem.ListViewSubControlItem)item.SubItems[0]).Control).Text;
                                if (int.Parse(LineNume) >= Max)
                                    Max = int.Parse(LineNume);
                            }

                            if (int.Parse(TRANS_LINE) <= Max && TRANS_LINE.Length >= 3)
                                TRANS_LINE = "00" + (Max + 1).ToString();
                            if (TRANS_LINE.Length == 3)
                                TRANS_LINE = NumBegin + "1";

                            //else if (int.Parse(TRANS_LINE) == Max)


                        }


                    }
                    catch
                    {
                        TRANS_LINE = NumBegin + NumEnd + "1";
                    }
                    
                    decimal newVal = nfPercent.Value;
                    decimal val = decimal.Parse((((ListViewItem.ListViewSubControlItem)objItem.SubItems[3]).Control).Text) - newVal;
                    if (val <= 0)
                    {
                        MessageBox.Show("Quantity number > 0 ");
                        return;
                    }
                    (((ListViewItem.ListViewSubControlItem)objItem.SubItems[3]).Control).Text = val.ToString();

                    decimal difVal = 0;
                    foreach (ListViewItem item in lvSplit.Items)
                    {
                        if (item.Index == objItem.Index)
                            difVal += newVal;
                        else difVal += decimal.Parse((((ListViewItem.ListViewSubControlItem)objItem.SubItems[3]).Control).Text);

                        //string LineNume = (((ListViewItem.ListViewSubControlItem)item.SubItems[0]).Control).Text;
                        //if (LineNume == TRANS_LINE.ToString())
                        //    TRANS_LINE = TRANS_LINE + "1";
                    }

                    if (val + difVal != txtAmount.Value)
                    {
                        val = txtAmount.Value - difVal;
                    }
                    objItem.Checked = false;
                    AddItem(TRANS_LINE, drItemCode["ITEM_CODE"].ToString(), drItemCode["SUPP_DESCR"].ToString(), newVal);
                }
                else
                {
                    MessageBox.Show("Quantity number > 0 ");
                    nfPercent.Focus();
                    return;
                }
            }
        }

        private void btRemove_Click(object sender, EventArgs e)
        {
            if (lvSplit.CheckedItems.Count > 0)
            {
                for (int i = lvSplit.CheckedItems.Count - 1 ; i >= 0; i--)
                {
                    ListViewItem objItem = lvSplit.CheckedItems[i] as ListViewItem;
                    if (objItem.Index != 0)
                    {
                        decimal val = decimal.Parse((((ListViewItem.ListViewSubControlItem)objItem.SubItems[3]).Control).Text);
                        ListViewItem preItem = lvSplit.Items[objItem.Index - 1];
                        ListViewItem.ListViewSubControlItem preSubItem = preItem.SubItems[3] as ListViewItem.ListViewSubControlItem;
                        ((Label)preSubItem.Control).Text = (decimal.Parse(((Label)preSubItem.Control).Text) + val).ToString();
                        lvSplit.Items.Remove(objItem);
                    }
                }

            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            string sErr = "";
            if (lvSplit.Items.Count > 1)
            {
                drItemOld = SetDataRow(dtPO.NewRow(), drItemCode);
                dtPO.Rows.Remove(drItemCode);
                for (int i = 0; i < lvSplit.Items.Count; i++)
                {
                    ListViewItem item = lvSplit.Items[i];
                    DataRow dr = dtPO.NewRow();
                    dr = SetDataRow(dr, drItemOld);
                    dr["TRANS_LINE"] = (((ListViewItem.ListViewSubControlItem)item.SubItems[0]).Control).Text.ToString();
                    dr["ITEM_CODE"] = (((ListViewItem.ListViewSubControlItem)item.SubItems[1]).Control).Text.ToString();
                    dr["SUPP_DESCR"] = (((ListViewItem.ListViewSubControlItem)item.SubItems[2]).Control).Text.ToString();
                    dr["PURCH_QTY"] = decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text);
                    dr["STOCK_QTY"] = decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text);
                    dr["AMOUNT"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["AMOUNT"].ToString()), 5);
                    dr["VAT"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VAT"].ToString()), 5);
                    dr["VALUE_1"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_1"].ToString()), 2);
                    dr["VALUE_2"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_2"].ToString()), 2);
                    dr["VALUE_3"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_3"].ToString()), 2);
                    dr["VALUE_4"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_4"].ToString()), 2);
                    dr["VALUE_5"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_5"].ToString()), 2);
                    dr["VALUE_6"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_6"].ToString()), 2);
                    dr["VALUE_7"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_7"].ToString()), 2);
                    dr["VALUE_8"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_8"].ToString()), 2);
                    dr["VALUE_9"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_9"].ToString()), 2);
                    dr["VALUE_10"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_10"].ToString()), 2);
                    dr["VALUE_11"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_11"].ToString()), 2);
                    dr["VALUE_12"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_12"].ToString()), 2);
                    dr["VALUE_13"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_13"].ToString()), 2);
                    dr["VALUE_14"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_14"].ToString()), 2);
                    dr["VALUE_15"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_15"].ToString()), 2);
                    dr["VALUE_16"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_16"].ToString()), 2);
                    dr["VALUE_17"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["VALUE_17"].ToString()), 2);
                    dr["OtherValue"] = Math.Round(decimal.Parse(((Label)((ListViewItem.ListViewSubControlItem)item.SubItems[3]).Control).Text) / txtAmount.Value * decimal.Parse(drItemOld["OtherValue"].ToString()), 5);
                    dtPO.Rows.Add(dr);
                    dtPO.AcceptChanges();
                    DataRow drReturn = dtReturn.NewRow();
                    dtReturn.Rows.Add(SetDataRow(drReturn, dr));
                    dtReturn.AcceptChanges();
                }
                DialogResult = Gizmox.WebGUI.Forms.DialogResult.OK;
                this.Close();
            }

        }

        public DataRow SetDataRow(DataRow dr, DataRow drItemOld)
        {
            dr["REC_TYPE"] = drItemOld["REC_TYPE"];
            dr["TRANS_REF"] = drItemOld["TRANS_REF"];
            dr["TRANS_LINE"] = drItemOld["TRANS_LINE"];
            dr["LOCATION"] = drItemOld["LOCATION"];
            dr["ITEM_CODE"] = drItemOld["ITEM_CODE"];
            dr["DEL_DATE"] = drItemOld["DEL_DATE"];
            dr["ITM_TYPE"] = drItemOld["ITM_TYPE"];
            dr["HIGH_REF"] = drItemOld["HIGH_REF"];
            dr["ORD_STATUS"] = drItemOld["ORD_STATUS"];
            dr["GRN_STATUS"] = drItemOld["GRN_STATUS"];
            dr["INV_STATUS"] = drItemOld["INV_STATUS"];
            dr["TRANS_CD"] = drItemOld["TRANS_CD"];
            dr["LINK_TEXT"] = drItemOld["LINK_TEXT"];
            dr["ID_ORDER"] = drItemOld["ID_ORDER"];
            dr["ID_GRN"] = drItemOld["ID_GRN"];
            dr["ID_INV"] = drItemOld["ID_INV"];
            dr["SUPP_DESCR"] = drItemOld["SUPP_DESCR"];
            dr["SUPP_ITEM_CODE"] = drItemOld["SUPP_ITEM_CODE"];
            dr["PURCH_QTY"] = drItemOld["PURCH_QTY"];
            dr["PURCH_UNIT"] = drItemOld["PURCH_UNIT"];
            dr["STOCK_QTY"] = drItemOld["STOCK_QTY"];
            dr["BASE_COST"] = drItemOld["BASE_COST"];
            dr["VALUE_1"] = drItemOld["VALUE_1"];
            dr["VALUE_2"] = drItemOld["VALUE_2"];
            dr["VALUE_3"] = drItemOld["VALUE_3"];
            dr["VALUE_4"] = drItemOld["VALUE_4"];
            dr["VALUE_5"] = drItemOld["VALUE_5"];
            dr["VALUE_6"] = drItemOld["VALUE_6"];
            dr["VALUE_7"] = drItemOld["VALUE_7"];
            dr["VALUE_8"] = drItemOld["VALUE_8"];
            dr["VALUE_9"] = drItemOld["VALUE_9"];
            dr["VALUE_10"] = drItemOld["VALUE_10"];
            dr["VALUE_11"] = drItemOld["VALUE_11"];
            dr["VALUE_12"] = drItemOld["VALUE_12"];
            dr["VALUE_13"] = drItemOld["VALUE_13"];
            dr["VALUE_14"] = drItemOld["VALUE_14"];
            dr["VALUE_15"] = drItemOld["VALUE_15"];
            dr["VALUE_16"] = drItemOld["VALUE_16"];
            dr["VALUE_17"] = drItemOld["VALUE_17"];
            dr["GRN_REF"] = drItemOld["GRN_REF"];
            dr["GRN_DATE"] = drItemOld["GRN_DATE"];
            dr["DEL_ADVISD"] = drItemOld["DEL_ADVISD"];
            dr["INV_DATE"] = drItemOld["INV_DATE"];
            dr["INV_PRD"] = drItemOld["INV_PRD"];
            dr["INV_APPR_DATE"] = drItemOld["INV_APPR_DATE"];
            dr["TRANS_TYPE"] = drItemOld["TRANS_TYPE"];
            dr["STOCK_UPD"] = drItemOld["STOCK_UPD"];
            dr["REASON"] = drItemOld["REASON"];
            dr["LINE_VAL"] = drItemOld["LINE_VAL"];
            dr["MATCH_VAL"] = drItemOld["MATCH_VAL"];
            dr["COST_VAL"] = drItemOld["COST_VAL"];
            dr["ACCNT_CODE"] = drItemOld["ACCNT_CODE"];
            dr["ASSET_CODE"] = drItemOld["ASSET_CODE"];
            dr["ASSET_SUB"] = drItemOld["ASSET_SUB"];
            dr["ASSET_IND"] = drItemOld["ASSET_IND"];
            dr["RECEIPT_REF"] = drItemOld["RECEIPT_REF"];
            dr["AUDIT_DATE"] = drItemOld["AUDIT_DATE"];
            dr["ANAL_M0"] = drItemOld["ANAL_M0"];
            dr["ANAL_M1"] = drItemOld["ANAL_M1"];
            dr["ANAL_M2"] = drItemOld["ANAL_M2"];
            dr["ANAL_M3"] = drItemOld["ANAL_M3"];
            dr["ANAL_M4"] = drItemOld["ANAL_M4"];
            dr["ANAL_M5"] = drItemOld["ANAL_M5"];
            dr["ANAL_M6"] = drItemOld["ANAL_M6"];
            dr["ANAL_M7"] = drItemOld["ANAL_M7"];
            dr["ANAL_M8"] = drItemOld["ANAL_M8"];
            dr["ANAL_M9"] = drItemOld["ANAL_M9"];
            dr["UNAPP_PRD_POSTED"] = drItemOld["UNAPP_PRD_POSTED"];
            dr["ORDER_DATE"] = drItemOld["ORDER_DATE"];
            dr["STOCK_PRD_POSTED"] = drItemOld["STOCK_PRD_POSTED"];
            dr["PERIOD_ORDERED"] = drItemOld["PERIOD_ORDERED"];
            dr["AMEND_DATE"] = drItemOld["AMEND_DATE"];
            dr["ORIG_LINE_NO"] = drItemOld["ORIG_LINE_NO"];
            dr["MOVEMENT_VALUE"] = drItemOld["MOVEMENT_VALUE"];
            dr["BCHECK_VALUE"] = drItemOld["BCHECK_VALUE"];
            dr["EXP_CHECK_VALUE"] = drItemOld["EXP_CHECK_VALUE"];
            dr["MISC_DOC_PRINT1"] = drItemOld["MISC_DOC_PRINT1"];
            dr["MISC_DOC_PRINT2"] = drItemOld["MISC_DOC_PRINT2"];
            dr["MISC_DOC_PRINT3"] = drItemOld["MISC_DOC_PRINT3"];
            dr["MISC_DOC_PRINT4"] = drItemOld["MISC_DOC_PRINT4"];
            dr["MISC_DOC_PRINT5"] = drItemOld["MISC_DOC_PRINT5"];
            dr["ADD_COST_VOUCHER1"] = drItemOld["ADD_COST_VOUCHER1"];
            dr["ADD_COST_VOUCHER2"] = drItemOld["ADD_COST_VOUCHER2"];
            dr["ADD_COST_VOUCHER3"] = drItemOld["ADD_COST_VOUCHER3"];
            dr["ADD_COST_VOUCHER4"] = drItemOld["ADD_COST_VOUCHER4"];
            dr["AMOUNT"] = drItemOld["AMOUNT"];
            dr["VAT"] = drItemOld["VAT"];
            dr["OtherValue"] = drItemOld["OtherValue"];
            dr["ItemDesc"] = drItemOld["ItemDesc"];
            return dr;
        }
    }
}